#!/bin/sh

# replace "steam" with the user you created, leave it alone if you've actually called your user "steam"
SRCDS_USER="steam"

# Do not change this path
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# The path to the game, only change this if you have a different installation path than in the guide.
DIR=/home/steam/Steam/steamapps/common/l4d2
DAEMON="$DIR/srcds_run"

# Change all Parameters to your needs.

############################################## TICKRATE INFO ########################################################
#
# Only 128 Tickrate and up will need modifications to the frametime and frametime_override, 100 tick and below do not need these parameters.
# 128 Tickrate needs the follows params added: -frametime 0.037 -frametime_override 0.037
#
#####################################################################################################################


############################################# PARAMETERS & SERVER.CFG ###############################################
#
# SVNUM will come in handy for when you're hosting multiple Servers on the same Dedicated Machine.
# Replace "1.3.3.7" with your Dedicated's Server IP.
# Replace 27015 with the Port this L4D2 Server will be hosted on.
#
# Rename your Server.cfg files accordingly, if you're hosting just one server, you'll only need server1.cfg
# If you're hosting multiple Servers, simply copy server1.cfg, change the hostname inside and rename it to server2.cfg and so on.
# Don't forget to copy and edit the file as well, the SVNUM has to match the server#.cfg and the Port has to be available.
#
#####################################################################################################################

# The current settings will start the Server on 100 Tick on Dead Center 1.
SVNUM=1
IP=1.3.3.7
PORT=27015
NAME=L4D2_Server$SVNUM
PARAMS="-game left4dead2 -ip $IP -port $PORT +sv_clockcorrection_msecs 15 -timeout 10 -tickrate 100 +map c1m1_hotel -maxplayers 32 +servercfgfile server$SVNUM.cfg"
DESC="L4D2 Dedicated Server #$SVNUM on port $PORT"


###########################################
#                                         #
#           DON'T TOUCH THESE             #
#                                         #
###########################################

case "$1" in
	start)
		echo "Starting $DESC: $NAME"
		if [ -e $DIR ]; then
			cd $DIR
			su $SRCDS_USER -l -c "screen -d -m -S $NAME $DAEMON $PARAMS"
		else
			echo "No such directory: $DIR!"
		fi
		;;

	stop)
		if su $SRCDS_USER -l -c "screen -ls" |grep $NAME; then
			echo -n "Stopping $DESC: $NAME"
			kill `su $SRCDS_USER -l -c "screen -ls" |grep $NAME |awk -F . '{print $1}'|awk '{print $1}'`
			echo " ... done."
		else
			echo "Couldn't find a running $DESC"
		fi
		;;

	restart)
		if su $SRCDS_USER -l -c "screen -ls" |grep $NAME; then
			echo -n "Stopping $DESC: $NAME"
			kill `su $SRCDS_USER -l -c "screen -ls" |grep $NAME |awk -F . '{print $1}'|awk '{print $1}'`
			echo " ... done."
		else
			echo "Couldn't find a running $DESC"
		fi
		echo -n "Starting $DESC: $NAME"
		cd $DIR
		su $SRCDS_USER -l -c "screen -d -m -S $NAME $DAEMON $PARAMS"
		echo " ... done."
		;;

	status)
		# Check whether there's a "srcds" process
		ps aux | grep -v grep | grep srcds_r > /dev/null
		CHECK=$?
		[ $CHECK -eq 0 ] && echo "SRCDS is UP" || echo "SRCDS is DOWN"
		;;

	*)
		echo "Usage: $0 {start|stop|status|restart}"
		exit 1
		;;
esac

exit 0



